package com.db.stexchem.server;

import com.db.stexchem.message.ExecutionReport;
import com.db.stexchem.message.Order;

public class Match {
  
  private ExecutionReport party;
  private ExecutionReport counterparty;
  
  public Match() {
  }
  
  private Match(final Order party, final Order counterparty,
      final float price, final int nShares) {
    this.party = new ExecutionReport();
    this.party.setOrderNo(party.getOrderNo());
    this.party.setPrice(price);
    this.party.setNShares(nShares);
    this.party.setCounterparty(counterparty.getParty());
    this.party.setFullyFilled(party.isFullyFilled()
        && nShares == party.getNShares());
    this.party.setOperation(party.getOperation());
    this.party.setStockName(party.getStockName());

    this.counterparty = new ExecutionReport();
    this.counterparty.setOrderNo(counterparty.getOrderNo());
    this.counterparty.setPrice(price);
    this.counterparty.setNShares(nShares);
    this.counterparty.setCounterparty(party.getParty());
    this.counterparty.setFullyFilled(counterparty.isFullyFilled()
        && nShares == counterparty.getNShares());
    this.counterparty.setOperation(counterparty.getOperation());
    this.counterparty.setStockName(counterparty.getStockName());
  }

  public ExecutionReport getParty() {
    return party;
  }

  public void setParty(ExecutionReport party) {
    this.party = party;
  }

  public ExecutionReport getCounterparty() {
    return counterparty;
  }

  public void setCounterparty(ExecutionReport counterparty) {
    this.counterparty = counterparty;
  }
  
  public static Match match(final Order party, final Order counterparty,
      final float price, final int nShares) {
    Match match = new Match(party, counterparty, price, nShares);
    party.setFullyFilled(false);
    party.setNShares(party.getNShares() - nShares);
    counterparty.setFullyFilled(false);
    counterparty.setNShares(counterparty.getNShares() - nShares);
    return match;
  }
}
